﻿using AutoMapper;
using Common.Result;
using Common.Utils;
using IService;
using Model.Dto.Employee;
using Model.Entitys;
using SqlSugar;

namespace Service
{
    public class EmplyeeService : IEmployeeService
    {
        private readonly ISqlSugarClient _db;
        private readonly IMapper _mapper;
        public EmplyeeService(ISqlSugarClient db, IMapper mapper)
        {
            _db = db;
            _mapper = mapper;
        }
        /// <summary>
        /// 根据id删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> DeleteEmployeeByIdAsync(long id)
        {
            int col = await _db.Deleteable<Employee>().Where(it => it.Id == id).ExecuteCommandAsync();
            return col > 0;
        }
        /// <summary>
        /// 修改账户
        /// </summary>
        /// <param name="employeeEdit"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> EditEmployeeAsync(EmployeeEdit employeeEdit, long id)
        {
            var emp = _mapper.Map<Employee>(employeeEdit);
            emp.UpdateUser = id;
            //更新忽略null
            return await _db.Updateable(emp).IgnoreColumns(ignoreAllNullColumns: true)
                .IgnoreColumns(it => new { it.CreateTime, it.CreateUser })
                .Where(it => it.Id == employeeEdit.Id).ExecuteCommandAsync() > 0;
        }
        /// <summary>
        /// 根据id获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<EmployeeRes> GetEmployeeByIdAsync(long id)
        {
            return _mapper.Map<EmployeeRes>(
                await _db.Queryable<Employee>().FirstAsync(it => it.Id == id)
                );
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageReq"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PageInfo> GetEmployeePageAsync(int page, int pageSize, string? name)
        {
            RefAsync<int> total = 0;
            var empList = await _db.Queryable<Employee>()
                .WhereIF(!string.IsNullOrEmpty(name), it => it.Name.Contains(name))
                .ToPageListAsync(page, pageSize, total);
            return new PageInfo()
            {
                Total = total,
                Data = empList
            };
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task<Employee> LoginAsync(EmployeeLoginReq loginReq)
        {
            var employee = await _db.Queryable<Employee>()
                .FirstAsync(it => it.Username.Equals(loginReq.Username)
                &&
                it.Password.Equals(MD5Helper.GetMd5Hash(loginReq.Password)));
            return employee;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="employeeAdd"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> SaveEmployeeAsync(EmployeeAdd employeeAdd, long id)
        { 
            var emp = _mapper.Map<Employee>(employeeAdd);
            //设置初始密码
            emp.Password = MD5Helper.GetMd5Hash("123456");
            //账号状态
            emp.Status = 1;
            emp.CreateUser = id;
            emp.UpdateUser = id;
            var res = await _db.Insertable(emp).ExecuteReturnSnowflakeIdAsync();
            if (res > 0) return true;
            return false;
        }
    }
}
